<template>
  <a-modal
    :open="open"
    width="700px"
    :title="rowData.templateName ? '修改病历模版' : '新增病历模版'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '120px' } }">
      <a-form-item label="病历文书" class="w-full">
        <div>{{ formState.categoryName }}</div>
      </a-form-item>
      <div class="flex w-full">
        <a-form-item label="模版名称" v-bind="validateInfos.templateName" class="w-2/3">
          <a-input v-model:value="formState.templateName" placeholder="请输入" @blur="getZjm" />
        </a-form-item>
        <a-form-item label="助记码" class="w-1/3" :label-col="{ style: { width: '80px' } }">
          <a-input v-model:value="formState.zjm" placeholder="请输入" />
        </a-form-item>
      </div>
      <a-form-item label="性别范围" class="w-full">
        <a-radio-group v-model:value="formState.sexRange">
          <a-radio value="0">不限</a-radio>
          <a-radio value="1">男</a-radio>
          <a-radio value="2">女</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="初诊" class="w-full">
        <a-radio-group v-model:value="formState.isFirst">
          <a-radio value="2">不限</a-radio>
          <a-radio value="1">初诊</a-radio>
          <a-radio value="0">复诊</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="共享范围" class="w-full">
        <a-radio-group v-model:value="formState.usableRange">
          <a-radio value="1">个人</a-radio>
          <a-radio value="2">科室</a-radio>
          <a-radio value="3">全院</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="备注" class="w-full mb-3">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useStore from '../store'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const store = useStore()

const formState = ref({
  templateName: ''
})
const rulesState = ref({
  templateName: [{ required: true, message: '请输入模版名称' }]
})
const { isCompleted, validateInfos, resetFields } = useForm(formState, rulesState)

const onOk = async () => {
  const { data, code, msg } = await post('emr/emrTemplate/doSaveEmrTemplete', {
    ...formState.value,
    templateId: props.rowData?.templateId || ''
  })
  if (code !== 0) return message.error(msg)
  store.templateId = data.id
  emit('success')
  onCancel()
}
const getZjm = async (e) => {
  const { data, code, msg } = await post('/common/util/getPinyin', {
    name: e.target.value
  })
  if (code !== 0) return message.error(msg)
  formState.value.zjm = data
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
  formState.value = {
    templateName: ''
  }
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData.id) {
        formState.value = {
          ...props.rowData
        }
      } else {
        formState.value = {
          ...props.rowData,
          id: '',
          sexRange: '0',
          isFirst: '2',
          usableRange: '1'
        }
      }
    }
  }
)
</script>
